<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use app\admin\model\Store as StoreModel;

class Store extends Base
{
    public function index()
    {
        $data = $this->request->param();

        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);


            if ($map == '') {
                $list = Db::name('store')->where('status', 0)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('sort desc,id asc')->limit($start, $limit)->select();
                $count = Db::name('store')->where('status', 0)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            } else {
                $list = Db::name('store')->where('status', 0)->where('is_del', 0)->order('sort desc,id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('store')->where('status', 0)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }


    public function add()
    {
        return $this->fetch();
    }

    //编辑栏目1-1
    public function edit($id)
    {
        $info = Db::name('store')->where('id', $id)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }
    //编辑栏目1-2
    public function save()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        $model = new StoreModel();
        if (empty($data['id'])) {
            $data['addtime'] = time();
            $data['enterprise_id'] = $this->EnterpriseId;
            $result = $model->allowField(true)->save($data);
            if ($result) {
                $status = 1;
                $msg = "添加成功";
            }
        } else {
            $id = $data['id'];
            $data['updatetime'] = time();
            $result = $model->allowField(true)->save($data, ["id" => $id]);
            if ($result) {
                $status = 1;
                $msg = "更新成功";
            }
        }
        return ['status' => $status, 'msg' => $msg];
    }

    //删除栏目1-1
    public function del($id)
    {
        $status = 0;
        $msg = "操作失败";
        $del = Db::name('store')->where('id', $id)->update(["is_del" => 1]);
        if ($del) {
            $status = 1;
            $msg = "删除成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }
}
